#include <iostream>
#include <cstdio>
using namespace std;
int n=0,a[5010],k[5010],p[5010];
int main ()
{
	while (scanf("%d",&a[++n])==1)
		k[n]=1,p[n]=0;
	int l,c;
	for (int i=n-1;i>=1;i--)
	{
		l=0,c=0;
		for (int j=i+1;j<=n;j++)
			if (a[j]<=a[i]&&l<k[j])
			{
				l=k[j];
				c=j;
			}
		if (l>0)
			k[i]=l+1,p[i]=c;
	}
	c=1;
	for (int j=1;j<=n;j++)
		if (k[j]>k[c])
			c=j;
	cout<<k[c]<<endl;
	cout<<a[c]<<" ";
	c=p[c];
	while (c!=0)
	{
		cout<<a[c]<<" ";
		c=p[c];
	}
	return 0;
}
